import cTitle from "components/title";
import { scrollMixin } from "utils/mixin";
import goodsItem from "../sachertorteComm/goodsItem";
export default {
  components: {
    cTitle,
    goodsItem
  },
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      goods: [],
      purchaseTime: "",
      timeData: {},
      page: 1,
      isLoadMore: true,
      totalPage: 0,
      city: "",
      locationStatus: false
    };
  },
  activated() {
    if(this.$route.query.city) {
      this.city = this.$route.query.city;
    }
    this.fun
      .getLocation()
      .then(res => {
        console.log(res);
        this.locationStatus = true;
        if (this.city === "") {
          this.city = res.city;
        }
        this.recommendList();
      })
      .catch(err => {
        console.log(err);
        this.locationStatus = false;
        this.recommendList();
      });
  },
  methods: {
    goodsNavTo(id) {
      this.$router.push(this.fun.getUrl("sachertorteGoods", {}, { id: id, city: this.city }));
    },
    onTimeChange(e) {
      this.timeData = e.detail;
    },
    recommendList() {
      let query = {
        page: this.page,
        city: this.city
      };
      $http.get("plugin.yz-supply-cake.frontend.goods.goods.getGoodsByTimeEndAscGoods", query, "正在获取").then(
        response => {
          if (response.result == 1) {
            let res = response.data;
            this.isLoadMore = true;
            this.totalPage = res.last_page;
            if (!this.totalPage) {
              this.page = 0;
            }
            this.goods = res.data;
            let endtime = new Date(this.goods[0]?.promotion_end_at.replace(/-/g,'/')).getTime();
            let current = new Date().getTime();
            this.purchaseTime = endtime - current
          } else {
            this.page = this.page - 1;
            this.isLoadMore = false;
            this.$dialog.alert({ message: response.msg });
          }
        },
        function(response) {
          // console.log(response);
          // error callback
        }
      );
    },
    //获取更多数据
    getMoreData() {
      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.page >= this.totalPage) {
        return;
      } else {
        this.page = this.page + 1;
        let query = {
          page: this.page,
          city: this.city
        };
        $http.post("plugin.yz-supply-cake.frontend.goods.goods.getGoodsByTimeEndAscGoods", query, "加载中").then(
          res => {
            this.isLoadMore = true;
            if (res.result === 1) {
              let myData = res.data.data;
              this.goods = [...this.goods, ...myData]; //数组拼接
            } else {
              this.query.page = this.query.page - 1;
              this.isLoadMore = false;
            }
          },
          function(response) {
            console.log("response", response);
            // error callback
          }
        );
      }
    }
  }
};
